<template>
  <div class="personal-main" style="margin-top: 15px; margin-left: 15px">
    <el-form :model="searchObj" :inline="true" class="demo-form-inline">
      <el-form-item label="关键字">
        <el-input
          v-model="searchObj.keyword"
          placeholder="手机号/姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="还款方式">
        <el-select
          v-model="searchObj.returnMethod"
          placeholder="请选择活动区域"
        >
          <el-option
            v-for="item in returnMethod"
            :key="item.id"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资金用途">
        <el-select v-model="searchObj.moneyUse" placeholder="请选择活动区域">
          <el-option
            v-for="item in moneyUse"
            :key="item.id"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="借款金额" required>
        <el-form-item>
          <el-input
            type="number"
            v-model="searchObj.amountStart"
            placeholder="开始"
          ></el-input>
        </el-form-item>
        -
        <el-form-item>
          <el-input
            type="number"
            v-model="searchObj.amountEnd"
            placeholder="结束"
          ></el-input>
        </el-form-item>
      </el-form-item>
      <el-form-item label="借款期限(月)" required>
        <el-form-item>
          <el-input
            type="number"
            v-model="searchObj.periodStart"
            placeholder="开始"
          ></el-input>
        </el-form-item>
        -
        <el-form-item>
          <el-input
            type="number"
            v-model="searchObj.periodEnd"
            placeholder="结束"
          ></el-input>
        </el-form-item>
      </el-form-item>
      <el-form-item label="申请时间" required>
        <el-form-item>
          <el-date-picker
            type="date"
            placeholder="开始"
            v-model="searchObj.createTimeStart"
            style="width: 100%"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        -
        <el-form-item>
          <el-date-picker
            placeholder="结束"
            v-model="searchObj.createTimeEnd"
            style="width: 100%"
            type="date"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onClear" type="success">清空</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="borrowInfoList" style="width: 100%">
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="name" label="借款人姓名"> </el-table-column>
      <el-table-column prop="amount" label="借款金额"> </el-table-column>
      <el-table-column prop="period" label="期数"> </el-table-column>
      <el-table-column prop="borrowYearRate" label="年化率"> </el-table-column>
      <el-table-column prop="returnMethod" label="还款方式"> </el-table-column>
      <el-table-column prop="moneyUse" label="资金用途"> </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status=='审核中'" type="error">{{ scope.row.status }}</el-tag>
          <el-tag v-else type="success">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="申请时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-col>
              <el-button @click="onShow(scope.$index, scope.row)" type="success"
                >查看</el-button
              >
            </el-col>
            <el-col>
              <el-button
              v-if="scope.row.status == '审核中'"
                @click="onApproval(scope.$index, scope.row)"
                type="success"
                >审批</el-button
              >
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[2, 5, 10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-dialog title="审批" :visible.sync="dialogFormVisible">
      <el-form :model="approval">
        <el-form-item label="是否通过" >
          <el-radio v-model="approval.status" :label="2">通过</el-radio>
          <el-radio v-model="approval.status" :label="-1">不通过</el-radio>
        </el-form-item>

        <el-form-item
          v-if="approval.status == 2"
          label="标的名称"
          
        >
          <el-input v-model="approval.title"></el-input>
        </el-form-item>
        <el-form-item
          label="起息日"
          v-if="approval.status == 2"
          
        >
          <el-date-picker
            v-model="approval.lendStartDate"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          v-if="approval.status == 2"
          label="年利化率"
          
        >
          <el-input v-model="approval.lendYearRate">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item
          v-if="approval.status == 2"
          label="服务费率"
          
        >
          <el-input v-model="approval.serviceRate">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
        <el-form-item
          v-if="approval.status == 2"
          label="标的描述"
          
        >
          <el-input v-model="approval.lendInfo" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="审批备注" >
          <el-input v-model="approval.content" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleCommit">确 定</el-button>
      </div>
    </el-dialog>
    
  </div>
</template>

<script>
import borrowApi from "@/api/borrow";
import dictApi from "@/api/dict";
export default {
  data() {
    return {
      approval: {
        status: 2,
        serviceRate: 5,
        lendYearRate: 0,
      },
      dialogFormVisible: false,
      searchObj: {},
      borrowInfoList: [],
      pageNum: 1,
      pageSize: 2,
      total: 0,
      returnMethod: [],
      moneyUse: [],
      borrowInfoId: 0
    };
  },
  methods: {
    handleCommit(){
      borrowApi.borrowInfoApproval(this.approval,this.borrowInfoId).then(r=>{
        this.$message.success(r.message);
        this.dialogFormVisible =false;
      })
    },
    onClear() {
      (this.pageNum = 1),
        (this.pageSize = 2),
        (this.searchObj = {}),
        this.onSearch();
    },
    onSearch() {
      borrowApi
        .getBorrowInfoList(this.pageNum, this.pageSize, this.searchObj)
        .then((r) => {
          this.borrowInfoList = r.data.page.records;
          this.total = Number(r.data.page.total);
        });
    },
    onShow(index, row) {
      this.$router.push(`/borrower/borrow/detail/${row.borrowInfoId}`);
    },
    onApproval(index, row) {
      // console.log(row);
      this.approval.lendYearRate = Number(row.borrowYearRate.replace('%','')); 
      this.borrowInfoId = row.borrowInfoId
      this.dialogFormVisible = true;
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.initData();
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.initData();
    },
    initData() {
      borrowApi
        .getBorrowInfoList(this.pageNum, this.pageSize, this.searchObj)
        .then((r) => {
          this.borrowInfoList = r.data.page.records;
          this.total = Number(r.data.page.total);
        });
      dictApi.listByCode("returnMethod").then((r) => {
        this.returnMethod = r.data.items;
      });
      dictApi.listByCode("moneyUse").then((r) => {
        this.moneyUse = r.data.items;
      });
    },
  },
  created() {
    this.initData();
  },
};
</script>